import pandas
import os
import matplotlib.pyplot as plt
import numpy as np


# 读取 CSV 数据
baseDir = './2025_Problem_C_Data'
athletes = pandas.read_csv(os.path.join(baseDir, 'summerOly_athletes.csv'), encoding='utf-8', low_memory=False)

athletes = athletes.groupby('NOC').filter(lambda x: x['Year'].max() >= 2024) # athletes 中筛掉 2024 没有参赛的队伍

# 统计每个 NOC 各类奖牌的数量
medal_counts = athletes.groupby(['NOC', 'Medal']).size().unstack(fill_value=0)
medal_counts['Total'] = medal_counts['Gold'] + medal_counts['Silver'] + medal_counts['Bronze']
medal_counts = medal_counts.sort_values(['Gold', 'Silver', 'Bronze'], ascending=False)

# 筛选出从未有获得奖牌的 NOC
no_medals = medal_counts[medal_counts['Total'] == 0]
print(no_medals)

# 以 ANG 为例进行分析
ang_medals = medal_counts[medal_counts.index == 'ANG']
ang_athletes = athletes[
    (athletes['NOC'] == 'ANG') &
    (athletes['Year'] >= 2020)
    ]
print(ang_athletes)

# 分析 Handball/Women Team/Handball Women's Handball 项目的竞争，筛选出最近的 Handball 运动员
times = 6
handball_athletes = athletes[
    (athletes['Sport'] == 'Handball') &
    ((athletes['Event'] == 'Women') | (athletes['Event'] == 'Women Team') | (athletes['Event'] == 'Handball Women\'s Handball')) &
    (athletes['Year'] > 2024-times*4) & 
    (athletes['Medal'] != 'No medal')
    ]

print(handball_athletes)

# 统计涉及到的 Team 数量，计算项目的竞争比例（是否有霸榜 Team）
team_count = handball_athletes['Team'].nunique()
print(team_count)
print(times*3/team_count)



